package com.wf.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wf.dao.mapper.ArticleMapper;
import com.wf.dao.pojo.Article;
import com.wf.dao.pojo.SysUser;
import com.wf.service.ArticleService;
import com.wf.utils.UserThreadLocal;
import com.wf.vo.Result;
import com.wf.vo.params.ArticleParam;
import com.wf.vo.params.PageParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/articles")
@CrossOrigin(origins = {"http://localhost:5173", "http://localhost:5174"})
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 首页 文章列表
     * @return
     */
    @PostMapping
    public Result getArticlesByPage(@RequestBody PageParam pageParam){
        int page = pageParam == null ? 1 : pageParam.getPage();
        int pageSize = pageParam == null ? 3 : pageParam.getPageSize();
        return articleService.getArticleList(page,pageSize);
    }

    @PostMapping("/publish")
    public Result publish(@RequestBody ArticleParam articleParam){
        log.info("前端传入插入文章"+articleParam.toString());
//        return Result.success("成功");
        return articleService.publish(articleParam);
    }

    @DeleteMapping("/{articleId}")
    public Result deleteArticle(@PathVariable Integer articleId){
        if (articleId == null){
            return Result.fail(401,"传入参数为空");
        }
        Article article = articleMapper.selectById(articleId);
        if(article == null){
            return Result.fail(401,"未找到该文章id");
        }
        articleService.deleteArticleById(articleId);
        return Result.success("删除文章成功");
    }


    @PostMapping("/my")
    public Result getMyArticles(@RequestBody PageParam pageParam){
        SysUser sysUser = UserThreadLocal.get();
        int userId = sysUser.getId();
        log.info("用户"+userId+"查看我的所有文章");
        int page = pageParam == null ? 1 : pageParam.getPage();
        int pageSize = pageParam == null ? 3 : pageParam.getPageSize();
        List<Article> myArticleList = articleService.getMyArticleList(page, pageSize, userId);
        return Result.success(myArticleList);
    }

    @PostMapping("/hot")
    public Result hotArticle(){
        return articleService.hotArticle();
    }

    @PostMapping("/new")
    public Result newArticle(){
        return articleService.newArticle();
    }

    @PostMapping("/listArchives")
    public Result listArchives(){
        return articleService.listArchives();
    }

    @PostMapping("/view/{id}")
    public Result view(@PathVariable("id") Integer id){
        return articleService.findArticleById(id);
    }



}
